package com.baijiayun.sdk.core;

import com.baijiayun.sdk.exception.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;


/**
 * @author Smile
 */
public abstract class CommonService implements Config {
    public static final Logger logger = LoggerFactory.getLogger(CommonService.class);
    /**
     * 接口域名
     */
    protected String domainName;
    /**
     * 合作人ID
     */
    protected int partnerId;
    /**
     * 合作人KEY
     */
    protected String partnerKey;
    /**
     * 秘钥
     */
    protected String secretKey;
    /**
     * 连接时长
     */
    protected Integer connectTimeoutMills = 10000;
    /**
     * 读取时长
     */
    protected Integer readTimeoutMills = 1800000;

    public final <T> T getJsonService(Class<T> cls) throws ApiException {
        return JsonProxy.createProxy(cls, this);
    }

    public void generateHeader() throws ApiException {
    }

    /**
     * 设置配置
     *
     * @param key
     * @param value
     */
    @Override
    public void setConfig(String key, String value) {
        try {
            setField(CommonService.class.getDeclaredField(key), value);
        } catch (Exception e) {
            logger.info("Can not set [" + key + "] into config field: " + e.toString());
        }
    }

    /**
     * 设置字段值
     *
     * @param field
     * @param value
     * @throws Exception
     */
    private void setField(Field field, String value) throws Exception {
        field.setAccessible(true);
        Class<?> cls = field.getType();
        if (cls.equals(int.class)) {
            field.setInt(this, Integer.parseInt(value));
        } else if (cls.equals(long.class)) {
            field.setLong(this, Long.parseLong(value));
        } else if (cls.equals(boolean.class)) {
            field.setBoolean(this, Boolean.parseBoolean(value));
        } else if (cls.equals(Integer.class)) {
            field.set(this, Integer.parseInt(value));
        } else if (cls.equals(Long.class)) {
            field.set(this, Long.parseLong(value));
        } else if (cls.equals(Boolean.class)) {
            field.set(this, Boolean.parseBoolean(value));
        } else {
            field.set(this, value);
        }
    }

    public String getDomainName() {
        return domainName;
    }

    public void setDomainName(String domainName) {
        this.domainName = domainName;
    }

    public int getPartnerId() {
        return partnerId;
    }

    public void setPartnerId(int partnerId) {
        this.partnerId = partnerId;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getPartnerKey() {
        return partnerKey;
    }

    public void setPartnerKey(String partnerKey) {
        this.partnerKey = partnerKey;
    }

    public Integer getConnectTimeoutMills() {
        return connectTimeoutMills;
    }

    public void setConnectTimeoutMills(Integer connectTimeoutMills) {
        this.connectTimeoutMills = connectTimeoutMills;
    }

    public Integer getReadTimeoutMills() {
        return readTimeoutMills;
    }

    public void setReadTimeoutMills(Integer readTimeoutMills) {
        this.readTimeoutMills = readTimeoutMills;
    }
}
